<template>
  <el-form-item :label="textLabel+'：'">
    <el-date-picker v-if="timeType" v-model="query.queryTime" type="datetime" :placeholder="'请选择'+textLabel" clearable></el-date-picker>
    <el-date-picker
      v-else
      v-model="query.queryTimes"
      type="daterange"
      range-separator="至"
      start-placeholder='起始日期'
      end-placeholder='结束日期'
      :clearable="clearable">
    </el-date-picker>
  </el-form-item>
</template>

<script>
export default {
  props:{
    text:{
      type:Array,
      default:function(){
        return []
      }
    },
    textLabel:{
      type:String,
      default:''
    },
    timeType:{
      type:Boolean,
      default:false
    },
    //true为单个，false多个
    isClear:{
      type:Boolean,
      default:false
    },
    clearable:{
      type:Boolean,
      default:true
    },
  },
  mounted(){
    let today = new Date(this.dateToStr(new Date()))
    let start = today
    let end = new Date(today.getTime() + 1000 * 60 * 60 * 24-1)
    if(this.timeType){
      this.$set(this.query, "queryTime", start)
      this.$emit("getData",this.formatTime(this.query.queryTime),this.text[0])
    }else{
      this.$set(this.query, "queryTimes", [start,end])
      this.$emit("getData",this.formatTime(this.query.queryTimes[0]),this.text[0])
      this.$emit("getData",this.formatTime(this.query.queryTimes[1]),this.text[1])
    }
    this.$emit("searchChange")
  },
  watch:{
    'query.queryTime':function(n){
      if(n){
        this.$emit("getData",this.formatTime(n),this.text[0])
      }else{
        this.$emit("getData",'',this.text[0])
      }
    },
    'query.queryTimes':{
      handler(n){
        if(n){
          this.$emit("getData",this.formatTime(n[0]),this.text[0])
          this.$emit("getData",this.formatTime(n[1]),this.text[1])
        }else{
          this.$emit("getData",'',this.text[0])
          this.$emit("getData",'',this.text[1])
        }
      },
      deep:true
    },
    'isClear':function(){
      if(this.timeType){
        this.query.queryTime=''
      }else{
        this.query.queryTimes=[]
      }
    },
  },
  data(){
    return{
      query:{
        queryTime:'',
        queryTimes:[]
      },
    }
  },
  methods:{
    //日期格式转换
    dateToStr(datetime){
      let year = datetime.getFullYear()
      let month = datetime.getMonth()
      let month1 = datetime.getMonth()+1//js从0开始取
      let date = datetime.getDate()

      if(month<10){
        month = "0" + month
      }
      if(month1<10){
        month1 = "0" + month1
      }
      if(date<10){
        date = "0" + date
      }
        return year + '/' + month1 + '/' + date
    },
    //对不符合标准的补零
    formatZero(x){
      return x<10?("0"+x):x
    },
    //格式化时间
    formatTime(time){
      let d=new Date(time);
      let tmpTime=d.getFullYear()+'-'+this.formatZero(d.getMonth()+1)+'-'+this.formatZero(d.getDate())+' '+this.formatZero(d.getHours())+':'+this.formatZero(d.getMinutes())+':'+this.formatZero(d.getSeconds());
      return tmpTime
    },
  },
}
</script>

<style>

</style>